home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 113_01 / a15symb.c < prev    next >
Text File  |  1985-03-09  |  4KB  |  174 lines

  1. /*
  2.     HEADER:        CUG113;
  3.     TITLE:        1802 Cross-Assembler (BDS C Version);
  4.     FILENAME:    A15SYMB.C;
  5.     VERSION:    1.2;
  6.     DATE:        07/22/1985;
  7.  
  8.     DESCRIPTION:    "This program lets you use your CP/M-80-based computer
  9.             to assemble code for the RCA 1802, 1804, 1805, 1805A,
  10.             1806, AND 1806A microprocessors.  The program is
  11.             written in BDS C for the best possible performance on
  12.             8-bit machines.  All assembler features are supported
  13.             except relocation, linkage, listing control, and
  14.             macros.";
  15.  
  16.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  17.             RCA, CDP1802, CDP1805A;
  18.  
  19.     SEE-ALSO:    CUG149, 1805A Cross-Assembler (Portable);
  20.  
  21.     SYSTEM:        CP/M-80;
  22.     COMPILERS:    BDS C;
  23.  
  24.     WARNINGS:    "This package is specifically tailored to CP/M-80
  25.             machines and the rather non-standard, but high-
  26.             performance BDS C compiler.  For other environments,
  27.             use the portable version of this package on CUG149.";
  28.  
  29.     AUTHORS:    William C. Colley III;
  30. */
  31.  
  32. /*
  33.     1805A Cross-Assembler  v 1.2
  34.  
  35.     Copyright (c) 1980, 82, 83, 85 William C. Colley, III.
  36.  
  37.     July 1982 -- Adapted from my 1802 cross-assembler.  WCC3.
  38.  
  39.     Vers 1.0 -- March 1983 -- Added 1805A opcodes to the 1805 set.  WCC3.
  40.  
  41.     Vers 1.1 -- March 1983 -- Added CPU pseudo-op to combine 1802 and 1805A
  42.             cross-assemblers into a single program.  WCC3.
  43.  
  44.     Vers 1.2 -- June 1985 -- Fixed IF block nesting mechanism bug and bug
  45.             in 1805A SCAL opcode.  WCC3.
  46.  
  47. File:    a15symb.c
  48.  
  49. Routines to manipulate the symbol table.
  50. */
  51.  
  52. /*  Get Globals:  */
  53.  
  54. #include "a15.h"
  55.  
  56. /*
  57. This function adds a new entry to the symbol table.  The function returns
  58. values of either 0 or -1.  If the value is 0, the symbol is already in the
  59. table and the global variable sympoint points to the existing entry.  If the
  60. value is -1, the symbol has just been entered into the table and sympoint
  61. points to the new entry.  If the symbol table is full, the function triggers
  62. an abort of the assembly.
  63. */
  64.  
  65. addsym(symbol)
  66. char *symbol;
  67. {
  68.     int t;
  69.  
  70.     if ((t = slookup(symbol)) > 0) wipeout("\nSymbol Table Overflow.\n");
  71.     if (t != 0) movmem(symbol, sympoint, SYMLEN);
  72.     return(t);
  73. }
  74.  
  75. /*
  76. This function checks the symbol table for a given symbol.  The function returns
  77. one of three values as follows:
  78.  
  79.      1 = symbol not found and symbol table full.
  80.      0 = symbol found.  sympoint points to the matching entry.
  81.     -1 = symbol not found.  sympoint points to where the symbol
  82.          should have been.
  83.  
  84. The symbol table is accessed using a hashing function with linear rehashing.
  85. */
  86.  
  87. slookup(symbol)
  88. char *symbol;
  89. {
  90.     int temp;
  91.  
  92.     temp = sympoint = &symtbl[hash(symbol)];
  93.     while((sympoint -> symname[0] & 0x7f) != '\0') {
  94.     if (symcmp(symbol,sympoint->symname) == 0) return(0);
  95.     if (++sympoint == symend) sympoint = symtbl;
  96.     if (sympoint == temp) return(1);
  97.     }
  98.     return(-1);
  99. }
  100.  
  101. /*
  102. This function returns a hash value for a given symbol.  The hash value
  103. is calculated by folding the symbol name up into 16 bits (2 bytes, thus
  104. the symbol length must be even) mod the number of symbols.
  105. */
  106.  
  107. hash(symbol)
  108. char *symbol;
  109. {
  110.     char i;
  111.     unsigned j;
  112.  
  113.     for (i = j = 0; i < (SYMLEN / 2); i++)
  114.     j += (*symbol++ << 8) + *symbol++;
  115.     return(j % SYMBOLS);
  116. }
  117.  
  118. /*
  119. Function to sort the symbol table.  The function
  120. returns the number of entries in the table.
  121. */
  122.  
  123. sortsym()
  124. {
  125.     int n, symcmp();
  126.     struct symbtbl *tptr;
  127.  
  128.     n = 0;
  129.     for (tptr = sympoint = symtbl; tptr < symend; ++tptr) {
  130.     if (tptr -> symname[0] & 0x7f) {
  131.         movmem(tptr->symname,(sympoint++)->symname,(SYMLEN+2));  ++n;
  132.     }
  133.     }
  134.     qsort(&symtbl,n,(SYMLEN+2),&symcmp);  return n;
  135. }
  136.  
  137. /*
  138. This function compares two symbols.  It returns zero if the
  139. symbols are the same, not zero if they are different.
  140. */
  141.  
  142. symcmp(sym1,sym2)
  143. char *sym1, *sym2;
  144. {
  145.     char i;
  146.     int t;
  147.  
  148.     for (i = 0; i < SYMLEN; i++)
  149.     if ((t = (*sym1++ & 0x7f) - (*sym2++ & 0x7f)) != 0) break;
  150.     return(t);
  151. }
  152.  
  153. /*
  154. Function to abort an assembly.  The parameter reason holds a string that
  155. will be printed to explain why the assembly bombed.  Note that I can't just
  156. call exit since this will not restore the currently logged disk drive, and
  157. software that changes the currently logged disk drive annoys me greatly.
  158. */
  159.  
  160. wipeout(reason)
  161. char *reason;
  162. {
  163.     puts(reason);  exit();
  164. }
  165. ies in the table.
  166. */
  167.  
  168. sortsym()
  169. {
  170.     int n, symcmp();
  171.     struct symbtbl *tptr;
  172.  
  173.     n = 0;
  174.     for (t